<?php
class Tourism_goods_category_model extends BZ_Model
{
    protected $table = 'tourism_goods_category';

    //分页--类别数量
    public function total($parent_id = 0, $status = 1)
    {
        $this->db->where('parent_id', $parent_id);
        $this->db->where('status', $status);
        return $this->db->count_all_results($this->table);
    }

    //分页--条目显示
    public function page_list($parent_id = 0, $status = 1)
    {
        $this->db->where('parent_id', $parent_id);
        $this->db->where('status', $status);
        return $this->db->get($this->table);
    }

    //根据id获取一天类别信息
    public function getCategoryById($id)
    {
        $this->db->where('id', $id);
        $this->db->limit(1);
        $result = $this->db->get($this->table)->result();
        if ($result[0]) {
            return $result[0];
        }
        return;
    }

    // 添加类别信息
    public function  add_category($param)
    {
        $this->db->insert($this->table, $param);
        $insert_id = $this->db->insert_id();
        if ($insert_id) {
            return true;
        }
        return false;
    }

    //更新类别信息
    public function update_category($param, $set)
    {

        $this->db->where('id', $set['id']);
        return $this->db->update($this->table, $param);

    }
    //类别删除
    /* public function delete($id){
         $ids = $this->getChildrenIds($id);
         $ids = $id.$ids;
         $id_array = explode(',',$ids);
         $this->db->where_in('id',$id_array);
         return  $this->db->delete($this->table);
     }*/
    //获取其子类的ids
    function getChildrenIds($id)
    {
        $ids = '';
        $this->db->where('parent_id', $id);
        $fruit = $this->db->get($this->table);

        if ($fruit->result()) {
            foreach ($fruit->result() as $item) {
                $ids .= ',' . $item->id;
                $ids .= $this->getChildrenIds($item->id);
            }
        }
        return $ids;
    }

    //递归调用
    function getLists($pid = 0, &$lists = array(), $deep = 1)
    {
        $this->db->where('parent_id', $pid);
        $result = $this->db->get('tourism_goods_category');
        foreach ($result->result() as $item) {
            $item->name = str_repeat('&nbsp;&nbsp;&nbsp;', $deep) . '|---' . $item->name;
            $lists[] = $item;
            $this->getLists($item->id, $lists, ++$deep);
            --$deep;
        }
        return $lists;
    }

    //显示select
    public function displayLists($pid = 0, $selectid = 1, $type = 0)
    {
        $result = $this->getLists($pid);
        $str = '<select name = "parent_id" autocomplete="off">';
        if ($type != 0) {
            $str .= '<option value = “０”>顶级分类</option>';
        }
        foreach ($result as $item) {
            $selected = "";
            if ($selectid == $item->id) {
                $selected = 'selected';
            }
            $str .= '<option value = "' . $item->id . '"' . $selected . '>' . $item->name . '</option>';
        }
        return $str .= '</select>';
    }

    public function classValidate($class)
    {
        $this->db->where('id', $class);
        $row = $this->db->get($this->table)->row();
        if ($row->level == 3) {
            return true;
        }
        return false;
    }

    //获取类别名称
    public function getCategoryNameById($id)
    {
        $this->db->select('name');
        $this->db->where('id', $id);
        $result = $this->db->get($this->table)->row_array();
        return $result['name'];
    }

    //获取父类id
    public function getParentId($childrenId)
    {
        $this->db->where('id', $childrenId);
        $result = $this->db->get($this->table)->row_array();
        if ($result['parent_id'] == 0) {
            return $result['id'];
        } else {
            return $this->getParentId($result['parent_id']);
        }
        return false;
    }

    //验证是否是一级分类 //
    public function isUltimate($cid)
    {
        $this->db->where('parent_id', $cid);
        return $this->db->count_all_results($this->table);
    }

    //根据最后一级获取全称
    public function getCategoryByLastId($id, $str = array())
    {
        $this->db->where('id', $id);
        $result = $this->db->get($this->table)->row_array();
        if (!$result) {
            return '';
        }
        if ($result['parent_id'] == 0) {
            $str[] = $result['name'];
            return implode('>', array_reverse($str));
        } else {
            $str[] = $result['name'];
            return $this->getCategoryByLastId($result['parent_id'], $str);
        }
    }
}